from django.db import models
from django.utils import timezone


class KeyPart(models.Model):
    """关键备件库存模型"""
    
    # 物品名称
    item_name = models.CharField(max_length=200, verbose_name='物品名称')
    
    # 型号
    model = models.CharField(max_length=100, blank=True, verbose_name='型号')
    
    # 入库数量
    stock_in_quantity = models.IntegerField(default=0, verbose_name='入库数量')
    
    # 入库时间
    stock_in_time = models.DateTimeField(null=True, blank=True, verbose_name='入库时间')
    
    # 库存数量
    inventory_quantity = models.IntegerField(default=0, verbose_name='库存数量')
    
    # 领用数量
    used_quantity = models.IntegerField(default=0, verbose_name='领用数量')
    
    # 领用时间
    used_time = models.DateTimeField(null=True, blank=True, verbose_name='领用时间')
    
    # 用途
    purpose = models.CharField(max_length=200, blank=True, verbose_name='用途')
    
    # 领用人
    user_name = models.CharField(max_length=100, blank=True, verbose_name='领用人')
    
    # 备注
    remarks = models.TextField(blank=True, verbose_name='备注')
    
    # 创建时间和更新时间
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'key_parts'
        verbose_name = '关键备件'
        verbose_name_plural = '关键备件'
        ordering = ['-created_at']
    
    def __str__(self):
        return f"{self.item_name} - {self.model}"
